﻿using Dapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SingleConfigTest.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Xman.Dapper.Sqlserver;

namespace SingleConfigTest.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly TestDapper _dapper;
        private readonly RisDapper _risdapper;

        public HomeController(ILogger<HomeController> logger, TestDapper dapper, RisDapper risdapper)
        {
            _logger = logger;
            _dapper = dapper;
            _risdapper = risdapper;
        }

        public IActionResult Index()
        {
            Trace.CorrelationManager.ActivityId = Guid.NewGuid();//对于不是web项目的程序 添加此语句在对应的地方
            var list = _dapper.GetListAsync<RequestInfo>("select top 100 * from RequestInfo").GetAwaiter().GetResult();
            var list2 = _risdapper.GetListAsync<RequestInfo>("select top 100 * from RequestInfo").GetAwaiter().GetResult();
            var list3 = new List<TranModel>();
            list3.Add(new TranModel()
            {
                Func = async (t,result) => { return await DeleteRequestFeeAsync("2131231", t); },
                MethodName = "DeleteRequestExamAsync"
            });
            _dapper.ExcuteTransAsync(list3).GetAwaiter().GetResult();
            _logger.LogInformation("213123123123213123123");
            _logger.LogInformation("8563873782785275785");
            return View();
        }

        private async Task<int> DeleteRequestFeeAsync(string sheetID, SqlTransaction transaction)
        {
            var sql = $"DELETE FROM RequestFee WHERE SheetID='{sheetID}'";
            return await _dapper.ExcuteCommandAsync(sql, null, transaction);
        }


        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }

    public class RequestInfo
    {
        public string SheetID { get; set; }
    }
}
